package com.hiltony.web.controller;

import com.hiltony.web.model.Location;
import com.hiltony.web.model.Member;
import com.hiltony.web.model.Track;
import com.hiltony.web.service.MemberService;
import com.hiltony.web.service.TrackService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;


/**
 * Created by YX on 2016/9/16.
 *
 */

@Controller
@RequestMapping("/track")
public class TrackController {

    @Resource
    private TrackService trackService;

    @Resource
    private MemberService memberService;

    @RequestMapping("/history")
    public String getTrack(Member member,HttpServletRequest request, Model model){

        SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");


        List<Member> memberList = memberService.getMemberList(new Member());
        Member currentMember = new Member();
        Map<String,List<Location>> trackControllerList;
        Location location = new Location();
        if (memberList.size()!=0){


            if (member!=null && member.getMemberId()!=null){
                currentMember=member;
                location.setMemberId(member.getMemberId());
            }
            else {
                currentMember=memberList.get(0);
                location.setMemberId(memberList.get(0).getMemberId());
            }
            if(member!=null && member.getTrackDayStr()!=null&&!member.getTrackDayStr().equals("")){
                try {
                    location.setDate(sdf.parse(member.getTrackDayStr()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            else {

                Location lday= trackService.getLatestDayByMemberId(currentMember);
                if (lday!=null){
                    location.setDate(lday.getSampleTime());
                }
            }
            trackControllerList =trackService.getTrackListByMemberId(location);
            model.addAttribute("currMember",currentMember);
            model.addAttribute("location",location);
            model.addAttribute("memberList",memberList);
            model.addAttribute("trackHistory", trackControllerList);
        }

        return "track/location_show";

    }


    @RequestMapping("/intime")
    public String getIntimeTrack(Member member,HttpServletRequest request, Model model){

        List<Member> memberList = memberService.getMemberList(new Member());
        Map<String,List<Location>> trackControllerList;
        Location location = new Location();
        if (memberList.size()!=0){
            trackControllerList =trackService.getIntimeTrackList(location);
            model.addAttribute("memberList",memberList);
            model.addAttribute("locationInTime", trackControllerList);
        }
        return "track/location_intime";

    }

}

